package com.telecomyt.jwportal;

import android.content.ComponentCallbacks2;
import android.content.Context;

import androidx.multidex.MultiDexApplication;

import com.bumptech.glide.Glide;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.telecomyt.jwportal.net.network.NetworkManager;
import com.tencent.bugly.crashreport.CrashReport;

import org.litepal.LitePal;


public class BaseApplication extends MultiDexApplication {
    private static BaseApplication application;
    //内存检测代码开始
    private RefWatcher refWatcher;

    public static RefWatcher getRefWatcher(Context context) {
        BaseApplication app = (BaseApplication) context.getApplicationContext();
        return app.refWatcher;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        application = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        //内存检测
        LitePal.initialize(getApplicationContext());
        if (LeakCanary.isInAnalyzerProcess(this)) {//1
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }

        refWatcher = LeakCanary.install(this);
        NetworkManager.getInstance().init(this);
//        RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
//            @Override
//            public void accept(Throwable throwable) throws Exception {
//                throwable.printStackTrace();//这里处理所有的Rxjava异常
//            }
//        });
        CrashReport.initCrashReport(getApplicationContext(), "bd249d3d88", true);
    }


    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
            Glide.get(this).clearMemory();
        }
        Glide.get(this).trimMemory(level);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        //内存低的时候清理glide缓存
        Glide.get(this).clearMemory();
    }

    public static BaseApplication getApplication() {
        return application;
    }


}
